package request_manage_models

import (
	"errors"
	"storeManageApi/models/dbModels"
	"storeManageApi/utils"
)

type NewGoodsConfig struct {
	Description string `json:"Description"`
	Model       string `json:"Model"`
	Name        string `json:"Name"`
	SafeNumber  int    `json:"SafeNumber"`
	StartNumber int    `json:"StartNumber"`
	Type        string `json:"Type"`
	Unit        string `json:"Unit"`
}

func (receiver NewGoodsConfig) ConvertGoodsConfig() dbModels.GoodsConfigs {
	return dbModels.GoodsConfigs{
		Base: dbModels.Base{
			Uid:         utils.RandomUid(),
			Description: receiver.Description,
		},
		Name:        receiver.Name,
		Model:       receiver.Model,
		Unit:        receiver.Unit,
		Type:        receiver.Type,
		SafeNumber:  receiver.SafeNumber,
		StartNumber: receiver.StartNumber,
	}
}

func (receiver NewGoodsConfig) Isvalidated() error {
	if receiver.Name == "" {
		return errors.New("物品名称不能为空")
	}
	if receiver.Model == "" {
		return errors.New("物品规格不能为空")
	}
	if receiver.Unit == "" {
		return errors.New("物品单位不能为空")
	}
	if receiver.SafeNumber < 0 {
		return errors.New("物品预警数不能小于0")
	}
	if receiver.Type == "" {
		return errors.New("物品类型不能为空")
	}
	if receiver.StartNumber < 0 {
		return errors.New("物品初始值不能小于0")
	}
	return nil
}
